﻿using QLBH.Models;
using QLBH.Modules;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Controllers
{
    public class CustomerController
    {
         //private WarehouseModel model;
        private ucCustomer view;
        public CustomerController(ucCustomer view)
        {
            //model = new WarehouseModel();
            this.view = view;
        }
        public void LoadData()
        {
            try
            {
                List<CustomerModel> data = WSAccess.GetData<List<CustomerModel>>(MainForm.Instance.Shop_id.ToString() + "/customers");
                view.DisplayData(data);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void Add()
        {
            try{
            
                frmAdd_Edit_Customer adddlg = new frmAdd_Edit_Customer(eType.ADD);
                if (adddlg.ShowDialog() == DialogResult.OK)
                {
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Edit()
        {
            try
            {
                frmAdd_Edit_Customer editdlg = new frmAdd_Edit_Customer(eType.EDIT);
                if (view.getCurrentSelectedItem() != null)
                    editdlg.Model = view.getCurrentSelectedItem();
                else
                    this.LoadData();
                if (editdlg.ShowDialog() == DialogResult.OK)
                {
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Delete()
        {
            try
            {
                if (MessageBox.Show("Bạn Có Muốn Xóa Không?", "Delete ConFirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    WSAccess.Delete<CustomerModel>(view.getCurrentSelectedItem().Id + "/customer/delete", view.getCurrentSelectedItem());
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
